package bjtuoj;

import java.util.Scanner;

/**
 * ClassName: Test3
 * Description:
 * date: 2021-11-12 19:12
 *
 * @author liyifan
 */
public class Test3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int vmax = scanner.nextInt();
            int mmax = scanner.nextInt();
            int N = scanner.nextInt();
            int[][] arr = new int[N][3];
            for (int i = 0;i < N;i++) {
                for (int j = 0;j < 3;j++) {
                    arr[i][j] = scanner.nextInt();
                }
            }
            int[][] dp = new int[450][450];
            for(int i = 0;i < N;i++){
                for(int j = vmax;j >= arr[i][0];j--){
                    for(int k = mmax;k >= arr[i][1];k--)
                        dp[j][k]=Math.max(dp[j][k], dp[j - arr[i][0]][k - arr[i][1]] + arr[i][2]);
                }
            }
            System.out.printf("%d\n",dp[vmax][mmax]);
        }
    }
}
